package com.pingtai.user.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.entity.UserSearch;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class UserManagerAction {

	public UserManagerService userManagerService;
	private PaginationSupport paginationSupport;
	private int currentPage;
	private int pageNum;
	private String queryString;
	
	private UserSearch qe;
	private String oldPassword;
	private String newPassword;
	
	private User user;
	private UserPay userPay;
	
	private List<UserIndentType> produceList;
	private List<UserIndentType> produceList2;
	private List<UserBaseIndentType> produceList3;
	private Map<String,String> produceMap=new HashMap<String, String>();
	private Map<String,String> produceMap2=new HashMap<String, String>();
	private User wm;
	
	private Integer userType;
	
	public List<UserBaseIndentType> getProduceList3() {
		return produceList3;
	}



	public void setProduceList3(List<UserBaseIndentType> produceList3) {
		this.produceList3 = produceList3;
	}



	public List<UserIndentType> getProduceList2() {
		return produceList2;
	}



	public void setProduceList2(List<UserIndentType> produceList2) {
		this.produceList2 = produceList2;
	}



	public Map<String, String> getProduceMap2() {
		return produceMap2;
	}



	public void setProduceMap2(Map<String, String> produceMap2) {
		this.produceMap2 = produceMap2;
	}



	public Map<String, String> getProduceMap() {
		return produceMap;
	}



	public void setProduceMap(Map<String, String> produceMap) {
		this.produceMap = produceMap;
	}



	public Integer getUserType() {
		return userType;
	}



	public void setUserType(Integer userType) {
		this.userType = userType;
	}



	public User getWm() {
		return wm;
	}



	public void setWm(User wm) {
		this.wm = wm;
	}



	public List<UserIndentType> getProduceList() {
		return produceList;
	}



	public void setProduceList(List<UserIndentType> produceList) {
		this.produceList = produceList;
	}



	public UserPay getUserPay() {
		return userPay;
	}



	public void setUserPay(UserPay userPay) {
		this.userPay = userPay;
	}



	public User getUser() {
		return user;
	}



	public void setUser(User user) {
		this.user = user;
	}



	public String getOldPassword() {
		return oldPassword;
	}



	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}



	public String getNewPassword() {
		return newPassword;
	}



	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}



	public UserSearch getQe() {
		return qe;
	}



	public void setQe(UserSearch qe) {
		this.qe = qe;
	}



	public int getPageNum() {
		return pageNum;
	}



	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}



	public int getCurrentPage() {
		return currentPage;
	}



	public void setCurrentPage(String currentPage) {
		this.currentPage = new Integer(currentPage);
	}



	public String getQueryString() {
		return queryString;
	}



	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}



	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}



	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}







	public UserManagerService getUserManagerService() {
		return userManagerService;
	}



	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}


	public String userManagerUI_sort_money(){
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
	    paginationSupport = userManagerService.getChildrenUsersForPage_sort(currentPage);
	    turn_to_page_sort=1;
		return "success";
	}
	
	private Integer turn_to_page_sort=0; //对余额排序用
	
	public Integer getTurn_to_page_sort() {
		return turn_to_page_sort;
	}

	public void setTurn_to_page_sort(Integer turn_to_page_sort) {
		this.turn_to_page_sort = turn_to_page_sort;
	}

	public String userManagerUIForSearch_sort_money(){
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			qe = userManagerService.paseQueryString(qe,queryString);
		}
		paginationSupport = userManagerService.getChildrenUsersForPage_sort(currentPage,qe);
		turn_to_page_sort=1;
		return "success";
	}

	public String userManagerUI(){
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
	    paginationSupport = userManagerService.getChildrenUsersForPage(currentPage);
		return "success";
	}
	
	public String userManagerUIForSearch(){
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			qe = userManagerService.paseQueryString(qe,queryString);
		}
		paginationSupport = userManagerService.getChildrenUsersForPage(currentPage,qe);
		return "success";
	}
	
	public String userPasswordUI(){
		return "success";
	}
	
	public String userChangePassword(){
		if(userManagerService.checkPassword(oldPassword)){
			userManagerService.updatePassword(newPassword);
		}else{
			return "pass_error";
		}
		return "success";
	}
	
	public String toUserRecharge(){
		user = userManagerService.findUserById(user.getUserId());
		return "success";
	}
	
	public String userRecharge(){
		
		userPay.setChildUser(user);
		int flag = userManagerService.saveUserPay(userPay,1);
		if(flag==1){
			return "success";
		}else if (flag==0) {
			return "NotEnoughMoney";
		}else if(flag==0){
			return "SaveException";
		}
		return "success";
	}
	
	public String toUserWithHold(){
		user = userManagerService.findUserById(user.getUserId());
		return "success";
	}
	
	public String userWithHold(){
		userPay.setChildUser(user);
		userPay.setMoney(userPay.getMoney()*(-1));
		int flag = userManagerService.saveUserPay(userPay,0);
		if(flag==1){
			return "success";
		}else if (flag==0) {
			return "NotEnoughMoney";
		}else if(flag==0){
			return "SaveException";
		}
		return "success";
	}
	
	public String toProduceOpen(){
		user = userManagerService.findUserById(user.getUserId());
		User cUser = userManagerService.findCurrectUser();
		if(!cUser.getUserId().equals(user.getParentUserId())){
			return "403";
		}
		produceList = userManagerService.getAllProduceList(user,cUser);
		produceList = Utils.menuSort2(produceList);
		produceList2 = userManagerService.getProduceList2();
		if(userManagerService.isRoot(cUser)){
			produceList3 = userManagerService.findAllBaseIndentType2();
			produceList3 = Utils.menuBaseSort(produceList3);
		}
		produceList2 = Utils.menuSort2(produceList2);
		
		for(int i=0;i<produceList.size();i++){
			UserIndentType indentType = produceList.get(i);
			if(indentType.getDefaultPrice()==null){
				indentType.setDefaultPrice(indentType.getPrice());
			}
		}
		
		return "success";
	}
	
//	public String toProduceOpen(){
//		user = userManagerService.findUserById(user.getUserId());
//		produceList = userManagerService.getProduceList(user);
//		produceList2 = userManagerService.getProduceList2();
//		produceList = Utils.menuSort(produceList);
//		produceList2 = Utils.menuSort(produceList2);
//		User cUser = userManagerService.findCurrectUser();
//		for(int i=0;i<produceList.size();i++){
//			UserIndentType type = produceList.get(i);
//			Set child = type.getChildrenIndentType();
//			if(child==null){
//				child = new HashSet();
//				List cl = userManagerService.getChildProduceList(cUser,type);
//				child.addAll(cl);
//				type.setChildrenIndentType(child);
//			}
//			if(type.getUserBaseIndentType().getChildrenBaseIndentType()!=null){
//				Set s = type.getUserBaseIndentType().getChildrenBaseIndentType();
//				Iterator it = s.iterator();
//				Set tmpSet = new HashSet();
//				while (it.hasNext()) {
//					boolean flag = false;
//					type.getUserBaseIndentType().setHasChild(true);
//					UserBaseIndentType b = (UserBaseIndentType) it.next();
//					if(child!=null&&child.size()>0) {
//						Iterator it2 = child.iterator();
//						while(it2.hasNext()){
//							UserIndentType t = (UserIndentType) it2.next();
//							if(t.getUserBaseIndentType().getId()==b.getId()){
//								flag = true;
//								break;
//							}
//						}
//					}
//					
//					if(!flag){
//						tmpSet.add(b);
//					}
//				}
//				type.getUserBaseIndentType().setChildrenBaseIndentType(tmpSet);
//			}
//		}
//		
//		for(int i=0;i<produceList2.size();i++){
//			UserIndentType type = produceList2.get(i);
//			Set child = type.getChildrenIndentType();
//			if(child==null){
//				child = new HashSet();
//				List cl = userManagerService.getChildProduceList(cUser,type);
//				child.addAll(cl);
//				type.setChildrenIndentType(child);
//			}
//			if(type.getUserBaseIndentType().getChildrenBaseIndentType()!=null){
//				Set s = type.getUserBaseIndentType().getChildrenBaseIndentType();
//				Iterator it = s.iterator();
//				Set tmpSet = new HashSet();
//				while (it.hasNext()) {
//					boolean flag = false;
//					type.getUserBaseIndentType().setHasChild(true);
//					UserBaseIndentType b = (UserBaseIndentType) it.next();
//					if(child!=null&&child.size()>0) {
//						Iterator it2 = child.iterator();
//						while(it2.hasNext()){
//							UserIndentType t = (UserIndentType) it2.next();
//							if(t.getUserBaseIndentType().getId()==b.getId()){
//								flag = true;
//								break;
//							}
//						}
//					}
//					
//					if(!flag){
//						tmpSet.add(b);
//					}
//				}
//				type.getUserBaseIndentType().setChildrenBaseIndentType(tmpSet);
//			}
//		}
//		
//		for(int i=0;i<produceList.size();i++){
//			List list = produceList.get(i).getChildrenIndentTypeList();
//			if(list!=null&&list.size()>0){
//				list = Utils.menuSort(list);
//				produceList.get(i).setChildrenIndentTypeList(list);
//			}
//		}
//		
//		for(int i=0;i<produceList2.size();i++){
//			List list = produceList2.get(i).getChildrenIndentTypeList();
//			if(list!=null&&list.size()>0){
//				list = Utils.menuSort(list);
//				produceList2.get(i).setChildrenIndentTypeList(list);
//			}
//		}
//		
//		if(userManagerService.isRoot(cUser)){
//			produceList3 = userManagerService.findAllBaseIndentType();
////			for(int i=0;i<produceList3.size();i++){
////				List list = produceList3.get(i).getChildrenIndentTypeList();
////				if(list!=null&&list.size()>0){
////					list = Utils.menuSort(list);
////					produceList3.get(i).setChildrenIndentTypeList(list);
////				}
////			}
//		}
//		return "success";
//	}
	
	public String produceOpen(){
		user = userManagerService.findUserById(user.getUserId());
		userManagerService.saveOrUpdateUserProduceType(produceList,user,produceMap,produceMap2);
		return "success";
	}
	
	public String toBatchProduceOpen(){
//		user = userManagerService.findCurrectUser();
//		produceList = userManagerService.getProduceList(user);
//		User cUser = userManagerService.findCurrectUser();
//		for(int i=0;i<produceList.size();i++){
//			UserIndentType type = produceList.get(i);
//			Set child = type.getChildrenIndentType();
//			if(child==null){
//				child = new HashSet();
//				List cl = userManagerService.getChildProduceList(cUser,type);
//				child.addAll(cl);
//				type.setChildrenIndentType(child);
//			}
//			if(type.getUserBaseIndentType().getChildrenBaseIndentType()!=null){
//				Set s = type.getUserBaseIndentType().getChildrenBaseIndentType();
//				Iterator it = s.iterator();
//				Set tmpSet = new HashSet();
//				while (it.hasNext()) {
//					boolean flag = false;
//					type.getUserBaseIndentType().setHasChild(true);
//					UserBaseIndentType b = (UserBaseIndentType) it.next();
//					if(child!=null&&child.size()>0) {
//						Iterator it2 = child.iterator();
//						while(it2.hasNext()){
//							UserIndentType t = (UserIndentType) it2.next();
//							if(t.getUserBaseIndentType().getId()==b.getId()){
//								flag = true;
//								break;
//							}
//						}
//					}
//					
//					if(!flag){
//						tmpSet.add(b);
//					}
//				}
//				type.getUserBaseIndentType().setChildrenBaseIndentType(tmpSet);
//			}
//		}
//		return "success";
		
		//user = userManagerService.findUserById(user.getUserId());
		User cUser = userManagerService.findCurrectUser();
//		if(cUser.getUserId()!=user.getParentUserId()){
//			return "403";
//		}
		produceList = userManagerService.getAllProduceList(cUser,cUser);
		produceList = Utils.menuSort2(produceList);
		produceList2 = userManagerService.getProduceList2();
		if(userManagerService.isRoot(cUser)){
			produceList3 = userManagerService.findAllBaseIndentType2();
			produceList3 = Utils.menuBaseSort(produceList3);
		}
		produceList2 = Utils.menuSort2(produceList2);
		
		for(int i=0;i<produceList.size();i++){
			UserIndentType indentType = produceList.get(i);
			if(indentType.getDefaultPrice()==null){
				indentType.setDefaultPrice(indentType.getPrice());
			}
		}
		
		return "success";
	}
	
	public String batchProduceOpen(){
		User curUser = userManagerService.findCurrectUser();
		boolean flag = userManagerService.isRoot(curUser);
		List produceOfCur = new ArrayList();
		if(flag){
			produceOfCur =  userManagerService.findAllBaseIndentType();
		}else{
			produceOfCur = new ArrayList(curUser.getUserIndentType());
		}
		List childUsers = userManagerService.findAllChildUsers();
		for(int i=0;i<childUsers.size();i++){
			User childUser = (User) childUsers.get(i);
			if(userManagerService.isAudit(childUser)) continue;
			userManagerService.saveOrUpdateUserProduceType2(produceOfCur,flag,curUser,produceList,childUser,produceMap,produceMap2);
		}
		return "success";
	}
	
	public String toAddUser(){
		return "success";
	}
	
	public String addUser(){
		if(userType==null) userType=0;
		int i = userManagerService.addUser(wm,userType);
		if(i==-1){
			return "userSame";
		}else if(i==403){
			return "403";
		}
		user = userManagerService.findUserByName(wm.getUserAccount());
		return "success";
	}
	
	public String toDefaultPrice(){
		User cUser = userManagerService.findCurrectUser();
		produceList2 = userManagerService.getProduceList2();
		produceList2 = Utils.menuSort2(produceList2);
		produceList2 = userManagerService.setBaseIndentTypeHasChild(produceList2);
		for(int i=0;i<produceList2.size();i++){
			UserIndentType indentType = produceList2.get(i);
			if(indentType.getDefaultPrice()==null){
				indentType.setDefaultPrice(indentType.getPrice());
			}
		}
		return "success";
	}
	
	public String defaultPrice(){
		userManagerService.updateUserIndentTypeDefaultPrice(produceList2);
		return "success";
	}

}
